package org.example;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.xddf.usermodel.chart.*;

import java.io.FileOutputStream;
import java.io.IOException;

public class ExcelFigureExample {

    public static void main(String[] args) {
        try {
            // 1. 创建工作簿和工作表
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = workbook.createSheet("图表示例");

            // 2. 准备图表数据
            createChartData(sheet);

            // 3. 创建柱状图
            createBarChart(workbook, sheet);

            // 4. 保存Excel文件
            try (FileOutputStream fos = new FileOutputStream("Excel图表示例.xlsx")) {
                workbook.write(fos);
                System.out.println("包含图表的Excel文件创建成功！");
            }

            // 5. 关闭工作簿
            workbook.close();
        } catch (IOException e) {
            System.err.println("创建Excel图表时出错: " + e.getMessage());
            e.printStackTrace();
        }
    }

    private static void createChartData(XSSFSheet sheet) {
        // 表头
        XSSFRow header = sheet.createRow(0);
        header.createCell(0).setCellValue("类别");
        header.createCell(1).setCellValue("值");

        // 数据行
        XSSFRow row1 = sheet.createRow(1);
        row1.createCell(0).setCellValue("A");
        row1.createCell(1).setCellValue(50);

        XSSFRow row2 = sheet.createRow(2);
        row2.createCell(0).setCellValue("B");
        row2.createCell(1).setCellValue(80);

        XSSFRow row3 = sheet.createRow(3);
        row3.createCell(0).setCellValue("C");
        row3.createCell(1).setCellValue(30);
    }

    private static void createBarChart(XSSFWorkbook workbook, XSSFSheet sheet) {
        // 创建绘图容器
        XSSFDrawing drawing = sheet.createDrawingPatriarch();

        // 设置图表位置和大小
        XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 3, 1, 10, 15);

        // 创建图表对象
        XSSFChart chart = drawing.createChart(anchor);

        // 创建坐标轴（必须先创建坐标轴）
        XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
        XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
        leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);

        // 创建数据源
        XDDFDataSource<String> categories = XDDFDataSourcesFactory.fromStringCellRange(
                sheet, new CellRangeAddress(1, 3, 0, 0));
        XDDFNumericalDataSource<Double> values = XDDFDataSourcesFactory.fromNumericCellRange(
                sheet, new CellRangeAddress(1, 3, 1, 1));

        // 创建图表数据
        XDDFChartData data = chart.createData(ChartTypes.BAR, bottomAxis, leftAxis);

        // 添加数据系列
        XDDFChartData.Series series = data.addSeries(categories, values);
        series.setTitle("销售额", null);

        // 绘制图表
        chart.plot(data);

        // 设置图例位置
        XDDFChartLegend legend = chart.getOrAddLegend();
        legend.setPosition(LegendPosition.TOP_RIGHT);

        // 调整列宽
        sheet.autoSizeColumn(0);
        sheet.autoSizeColumn(1);
    }
}